<template>
  <div class="role-container">
    <div class="search-box">
      <el-form size="mini" :model="searchForm" ref="searchFormRef" label-suffix=":" inline>
        <el-form-item label="角色编码" prop="roleName">
          <el-input v-model="searchForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="searchForm.roleDesc"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTimeRange">
          <el-date-picker type="daterange" v-model="searchForm.createTimeRange" format="YYYY-MM-DD"></el-date-picker>
        </el-form-item>
        <el-form-item label="修改时间" prop="updateTimeRange">
          <el-date-picker type="daterange" v-model="searchForm.updateTimeRange" format="YYYY-MM-DD"></el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="dataStatus">
          <el-select  v-model="searchForm.dataStatus" >
            <el-option :label="item.label" :value="item.value" :key="index"  v-for="(item,index) in dataStatusOptions"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item >
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button  @click="resetSearchForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="btn-wrapper">
      <el-button size="small" @click="visible = true"> 新建</el-button>
    </div>
    <div class="table-wrapper">
      <el-table :data="tableData" size="mini" v-loading="tableLoading" border stripe>
        <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
        <el-table-column  label="角色编码" prop="roleName"></el-table-column>
        <el-table-column  label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column  label="状态" prop="dataStatus">
          <template v-slot="{row}">
            <div>{{translateDictCode(row.dataStatus,dataStatusOptions)}}</div>
          </template>
        </el-table-column>
        <el-table-column  label="创建时间" prop="createTime"></el-table-column>
        <el-table-column  label="修改时间" prop="updateTime"></el-table-column>
        <el-table-column  label="操作">
          <template v-slot="{row}">
            <el-button type="text" size="mini" @click="editRole(row.id)">修改</el-button>
            <el-button type="text" size="mini" @click="delRole(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-wrapper">
      <el-pagination
          :current-page="searchForm.page"
          :page-size="searchForm.pageSize"
          :page-sizes="[10, 20, 30, 40,50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentPageChange"
          @size-change="handlePageSizeChange"
      ></el-pagination>
    </div>
    <el-dialog width="500px" v-model="visible" title="新增/修改" destroy-on-close :close-on-click-modal="false" @close="resetRoleForm">
      <el-form :model="roleForm" size="mini" :rules="rules" ref="roleFormRef" label-suffix="：" label-width="100px">
        <el-form-item label="菜单编码" prop="roleName">
          <el-input v-model="roleForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="菜单描述" prop="roleDesc">
          <el-input v-model="roleForm.roleDesc"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="dataStatus">
          <el-select  v-model="roleForm.dataStatus" style="width: 100%">
            <el-option :label="item.label" :value="item.value" :key="index"  v-for="(item,index) in dataStatusOptions"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单" >
         <el-tree
             ref="menuTreeRef"
             show-checkbox
             :data="menus"
             node-key="id"
             :default-checked-keys="roleForm.menuIds"
             :props="{label:'name'}"></el-tree>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button size="small" type="primary" @click="submit">提交</el-button>
        <el-button size="small" type="danger" @click="visible = false">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import {defineComponent} from 'vue'
import useSearchForm from "@/views/system/role/hooks/useSearch";
import useDataStatusOptions from "@/hooks/useDataStatusOptions/useDataStatusOptions";
import useTable from "@/views/system/role/hooks/useTable";
import useTranslateDictCode from "@/hooks/useTranslateDictCode/useTranslateDictCode";
import useForm from "@/views/system/role/hooks/useForm";
export default defineComponent({
  name:'Role',
  setup(){
    const {tableData,getRoleDataList,total,handleCurrentPageChange,handlePageSizeChange,tableLoading} = useTable()
    getRoleDataList()
    return {
      ...useSearchForm(),
      ...useDataStatusOptions(),
      ...useTranslateDictCode(),
      tableData,
      total,
      handleCurrentPageChange,
      handlePageSizeChange,
      tableLoading,
      ...useForm()
    }
  },
})
</script>
<style lang="scss" scoped>
.role-container{
  padding: 5px;
}
</style>
